<?php

namespace App\Http\Controllers;
use App\ErrorDesc;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\ErrorCode;
class Controller extends BaseController
{


    protected $_param;

    /**验证签名
     * @param $sign
     * @param $wait
     * @return bool
     */
    protected static function ValidateSign($sign, $wait){
        if (empty($sign) || empty($wait)){
            return false;
        }

        $waitVerify = '';

        array_walk($wait, function ($value)use(&$waitVerify){
           $waitVerify .= $value;
        });

        if (md5($waitVerify) !== $sign){
            return false;
        }

        return true;
    }
    protected function getParam($field){
        if (isset($this->_param->$field)){
            return $this->_param->$field;
        }else{
            return null;
        }
    }


    protected function setParam($request){
        $this->_param = (object)$request->input();
    }


    //test
    /**输出
     * @param $content
     * @param int $errorCode
     * @return array
     */
    protected function output($content, $errorCode = 0){

        $result = ['errorCode' => $errorCode];
        $errorDesc = new ErrorDesc();

        if (isset($errorDesc->desc[$errorCode])){
            $result['errorMsg'] = $errorDesc->desc[$errorCode];
        }else{
            $result['errorMsg'] = '未知错误类型';
        }

        if ((int)$errorCode == 0){
            $result['data'] = $content;
        }

        return $result;
    }

}
